package com.mini.receiver;

import com.mini.constant.MqConstants;
import com.mini.service.BusinessTraditionChanceService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

/**
 * @author shixiaopeng  on 2020/12/8
 */
@Component
@Slf4j
public class WechatChatReceiver {

    @Autowired
    BusinessTraditionChanceService businessTraditionChanceService;

    @RabbitListener(queues = MqConstants.TEACHER_SAY_QUEUE_NAME)
    @RabbitHandler
    public void receive(Channel channel, Message message) {
        //同步微信条数--咨询师发给学员
        String msg = null;
        try {
            msg = new String(message.getBody() , "UTF-8");
            log.info("接受msg消息：{}",msg);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        log.info("接受msg消息：{}",msg);
        //消息结构 teacherId + "@" + teacherWxId  + "@" + studentWxId + "@" + msgId
        String[] split = msg.split("@");
        // 统计今日微信条数到机会中
        try{
            businessTraditionChanceService.updateWechatRecord(split[2]);
        }catch (Exception ex){
            log.info("WechatChatReceiver 消息 消费失败 ={}",ex);
        }
    }
}
